﻿using System;
using System.Collections.Generic;
using System.Windows.Input;
using AzureTracer.Model;
using AzureTracer.Resources;
using AzureTracer.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Ioc;
using AzureTracer.Common;

namespace AzureTracer.ViewModels
{
    public class SettingsViewModel : ViewModelBase
    {
        public const string OKCANCELDIALOG_MESSAGE = "ShowOKCancelDialog";
        public const string CONFIRMDIALOG_MESSAGE = "ConfirmDialog";
 
        private IAppSettings _appSettings;
        private INavigationService _navigationService;
        private ILogService _logService;
        private IEmailService _emailService;

        #region Commands
        public ICommand SaveCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }
        public ICommand SignOutCommand { get; private set; }
        public ICommand SendFeedbackEmailCommand { get; private set; }
        #endregion

        public SettingsViewModel()
            :this(ViewModelLocator.AppSettings,ViewModelLocator.LogService,ViewModelLocator.NavigationService, ViewModelLocator.EmailService) {}

        [PreferredConstructor]
        public SettingsViewModel(IAppSettings appSettings, ILogService logService, INavigationService navigationService, IEmailService emailService)
        {
            _appSettings = appSettings;
            _logService = logService;
            _navigationService = navigationService;
            _emailService = emailService;

            _newContent = BuildNewContentList();
            _newContentSelection = GetNewContentItem(_appSettings.ScheduleTimeInMinutes);

            _traceFrom = BuildTraceFromList();
            _traceFromSelection = GetTraceFromItem(_appSettings.MaxDaysSync);

            SaveCommand = new RelayCommand(OnSave);
            CancelCommand = new RelayCommand(OnCancel);
            SignOutCommand = new RelayCommand(OnSignOut);
            SendFeedbackEmailCommand = new RelayCommand(OnSendFeedbackEmail);

            Messenger.Default.Register<bool>(this, CONFIRMDIALOG_MESSAGE, OnContinueSingOut);
        }

        #region NewContentSelection NotifyProperty
        private KeyValuePair<int, string> _newContentSelection;
        public KeyValuePair<int, string> NewContentSelection
        {
            get { return _newContentSelection; }
            set
            {
                _newContentSelection = value;
                RaisePropertyChanged("NewContentSelection");
            }
        }
        #endregion

        #region TraceFromSelection NotifyProperty
        private KeyValuePair<int, string> _traceFromSelection;
        public KeyValuePair<int, string> TraceFromSelection
        {
            get { return _traceFromSelection; }
            set
            {
                _traceFromSelection = value;
                RaisePropertyChanged("TraceFromSelection");
            }
        }
        #endregion

        private List<KeyValuePair<int, string>> _newContent;
        public List<KeyValuePair<int, string>> NewContent { get { return _newContent; } }

        private List<KeyValuePair<int, string>> _traceFrom;
        public List<KeyValuePair<int, string>> TraceFrom { get { return _traceFrom; } }

        #region Commands Events
        private void OnSendFeedbackEmail()
        {           
            string subjet = string.Format(string.Format("{0} v.{1} Feedback" ,labels.AppTitle, labels.AppVersion));

            _emailService.SentTo("alberto_fraj@hotmail.com",subjet, string.Empty);
        }

        private void OnSave()
        {
            _appSettings.ScheduleTimeInMinutes = _newContentSelection.Key;
            int newMaxDaysSync = _traceFromSelection.Key;
            if (_appSettings.MaxDaysSync != newMaxDaysSync)
            {
                _appSettings.MaxDaysSync = newMaxDaysSync;
                _appSettings.LastKeySync = string.Empty;
                _appSettings.LastTimeSync = DateTime.Today.AddMonths(-2);
            }

            _navigationService.Back();
        }

        private void OnCancel()
        {
            _navigationService.Back();
        }

        private void OnSignOut()
        {
            Messenger.Default.Send<string>
                   (labels.ConfirmSignOut, OKCANCELDIALOG_MESSAGE);
          
        }

        private void OnContinueSingOut(bool isSignOut)
        {
            if (!isSignOut) return;

            _appSettings.AccountName = string.Empty;
            _appSettings.AccountKey = string.Empty;
            _appSettings.LastKeySync = string.Empty;
            _appSettings.LastTimeSync = DateTime.Today.AddMonths(-2);
            _appSettings.AnySyncEnding = false;
            _logService.LogList.Clear();

            _navigationService.ToLogin();
        }

        #endregion

        #region Private Methods
        private KeyValuePair<int, string> GetTraceFromItem(int maxDaysSync)
        {
            KeyValuePair<int, string> selectedItem = _traceFrom[0];

            foreach (var item in _traceFrom)
                if (item.Key == maxDaysSync)
                {
                    selectedItem = item;
                    break;
                }
            return selectedItem;
        }

        private KeyValuePair<int, string> GetNewContentItem(int scheduleTimeInMinutes)
        {
            KeyValuePair<int, string> selectedItem = _newContent[0];

            foreach (var item in _newContent)
                if (item.Key == scheduleTimeInMinutes)
                {
                    selectedItem = item;
                    break;
                }
            return selectedItem;
        }

        private List<KeyValuePair<int, string>> BuildNewContentList()
        {
            var source = new List<KeyValuePair<int, string>>();
            source.Add(new KeyValuePair<int, string>(30, ResourceHelper.NewContent(30)));
            source.Add(new KeyValuePair<int, string>(60, ResourceHelper.NewContent(60)));
            source.Add(new KeyValuePair<int, string>(360, ResourceHelper.NewContent(360)));
            source.Add(new KeyValuePair<int, string>(0, ResourceHelper.NewContent(0)));
            return source;
        }

        private List<KeyValuePair<int, string>> BuildTraceFromList()
        {
            var source = new List<KeyValuePair<int, string>>();
            source.Add(new KeyValuePair<int, string>(1, ResourceHelper.TraceFrom(1)));
            source.Add(new KeyValuePair<int, string>(3, ResourceHelper.TraceFrom(3)));
            source.Add(new KeyValuePair<int, string>(7, ResourceHelper.TraceFrom(7)));
            source.Add(new KeyValuePair<int, string>(14, ResourceHelper.TraceFrom(14)));
            source.Add(new KeyValuePair<int, string>(31, ResourceHelper.TraceFrom(31)));
//            source.Add(new KeyValuePair<int, string>(90, ResourceHelper.TraceFrom(90)));
            return source;
        }
        #endregion
    }
}
